package com.xxxy.jsq.action.course;

import com.xxxy.jsq.dao.CourseDao;
import com.xxxy.jsq.model.Course;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class updateCourseServlet
 */
@WebServlet("/updateCourseServlet")
public class updateCourseServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public updateCourseServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        Course model = new Course();
        CourseDao dao = new CourseDao();

        // 获取页面传递的参数
        String courseName = request.getParameter("courseName");
        String courseNum = request.getParameter("courseNum");
        String courseScoreStr = request.getParameter("courseScore");
        String courseTimesStr = request.getParameter("courseTimes");
        String courseLtimesStr = request.getParameter("courseLtimes");
        String courseStimesStr = request.getParameter("courseStimes");
        String courseFlag = request.getParameter("courseFlag");
        Long courseId = Long.valueOf(request.getParameter("courseId"));

        // 设置更新的课程信息
        model.setCourseId(courseId);

        if (courseName != null && !courseName.equals("")) {
            model.setCourseName(courseName);
        }
        if (courseNum != null && !courseNum.equals("")) {
            model.setCourseNum(courseNum);
        }
        if (courseScoreStr != null && !courseScoreStr.equals("")) {
            model.setCourseScore(Double.parseDouble(courseScoreStr));
        }
        if (courseTimesStr != null && !courseTimesStr.equals("")) {
            model.setCourseTimes(Integer.parseInt(courseTimesStr));
        }
        if (courseLtimesStr != null && !courseLtimesStr.equals("")) {
            model.setCourseLtimes(Integer.parseInt(courseLtimesStr));
        }
        if (courseStimesStr != null && !courseStimesStr.equals("")) {
            model.setCourseStimes(Integer.parseInt(courseStimesStr));
        }
        if (courseFlag != null && !courseFlag.equals("")) {
            model.setCourseFlag(courseFlag);
        }

        // 设置更新时间
        model.setCourseStmp(new Timestamp(new Date().getTime()));

        // 执行更新操作
        String str = dao.update(model);

        // 返回操作结果
        PrintWriter out = response.getWriter();
        out.print(str);
        out.flush();
        out.close();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}